package com.zhuss.food.processor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.food.product.SearchProductBo;
import com.zhuss.bean.vo.food.product.ListProductVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.enums.UpperDownEnum;
import com.zhuss.enums.ProductType;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.food.db.dao.ext.ProductDao;
import com.zhuss.food.db.entity.Product;


/**
 * 查询商品 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/29 16:01
 */
@Component("productRead")
public class ProductRead {
	
	@Autowired
	private ProductDao productDao;
	
	
	public List<Product> listByFood(Long foodId) {
		List<Product> result = productDao.listByFood(foodId);
		return result;
	}
	
	public List<Product> listWeekNotId(Integer type, Long foodId, Date date,
			Integer productTime, Integer floor, Long productId, Integer area) {
		List<Product> result = new ArrayList<Product>();
		List<Product> list = listWeek(type, foodId, date, productTime, floor, area);
		for (Product c : list) {
			if (!c.getProductId().equals(productId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	public List<Product> listWeek(Integer type, Long foodId, Date date,
			Integer productTime, Integer floor, Integer area) {
		List<Product> result = productDao.listWeek(type, foodId, date, productTime, floor, area);
		return result;
	}
	
	/**
	 * 根据名字查询商品
	 * @param area 
	 * @param 
	 * @return
	 */
	public List<Product> list(Integer type, String name, Integer area) {
		List<Product> result = productDao.listByName(type, name, area);
		return result;
	}
	
	public List<Product> listNotId(Integer type, String name, Long productId, Integer area) {
		List<Product> result = new ArrayList<Product>();
		List<Product> list = list(type, name, area);
		for (Product c : list) {
			if (!c.getProductId().equals(productId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	
	/**
	 * 商品ID列表
	 * @param area
	 * @return
	 */
	public Object listProductId(Integer area) {
		List<Product> productList = productDao.listAll(area, null);
		List<Long> result = new ArrayList<>();
		for (Product product : productList) {
			result.add(product.getProductId());
		}
		return result;
	}
	
	/**
	 * 商品
	 * @param area
	 * @return
	 */
	public Object listProduct(Integer area) {
		List<Product> productList = productDao.listAll(area, null);
		List<ListVo> result = new ArrayList<>();
		transferList(productList, result);
		return result;
	}
	
	/**
	 * 商品 
	 * @param area
	 * @param status
	 * @return
	 */
	public Object listProductId(Integer area, Integer status) {
		List<Product> productList = productDao.listAll(area, status);
		List<Long> result = new ArrayList<>();
		transferListId(productList, result);
		return result;
	}
	
	private void transferListId(List<Product> productList, List<Long> result) {
		for (Product item : productList) {
			result.add(item.getProductId());
		}
	}

	private void transferList(List<Product> productList, List<ListVo> result) {
		for (Product item : productList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getProductId());
			vo.setLabel(item.getName());
			result.add(vo);
		}
		
	}

	/**
	 * 商品列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchProductBo bo) {
		PageVo<ListProductVo> result = new PageVo<ListProductVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(productDao.list(bo));
        result.setTotal(productDao.countList(bo));
		return result;
	}
	
	/**
	 * 商品详情
	 * @param productId
	 * @return
	 * @throws WpsException
	 */
	public Object detail(Long productId) throws WpsException {
		Object result = productDao.detail(productId);
		if (result == null) {
			throw new WpsException(ExceptionCode.数据不存在);
		}
		return result;
	}

	public List<Product> listProduct(Integer status, Integer type, Date startTime,
			Integer category, Date date, Integer area) {
		List<Product> result = productDao.listProduct(status, type, startTime, category, date, area);
		return result;
	}
	
	public List<Product> listStirfry(Integer category, Date date, Integer area) {
		List<Product> result = listProduct(UpperDownEnum.上架.key(), ProductType.点炒服务.key(), null, category, date, area);
		return result;
	}
	
	public List<Product> listBox(Date date, Integer area) {
		List<Product> result = listProduct(UpperDownEnum.上架.key(), ProductType.公务接待.key(), null, null, date, area);
		return result;
	}
	
	public List<Product> listTakeout(Date startTime, Date date, Integer area) {
		List<Product> result = listProduct(UpperDownEnum.上架.key(), ProductType.特价外卖.key(), startTime, null, date, area);
		return result;
	}
	
	public List<Product> listWeekMenu(Date date, Integer area) {
		List<Product> result = listMenu(UpperDownEnum.上架.key(), date, null, null, area, ProductType.一周菜单.key());
		return result;
	}
	
	public List<Product> listFloorMenu(Date date, Integer productTime, Integer floor, Integer area) {
		List<Product> result = listMenu(UpperDownEnum.上架.key(), date, productTime, floor, area, ProductType.一周菜单.key());
		return result;
	}
	
	public List<Product> listMenu(Integer status, Date date, Integer productTime, Integer floor, Integer area, Integer type) {
		List<Product> result = productDao.listMenu(status, date, productTime, floor, area, type);
		return result;
	}
	
	

}
